--autostruggle 

local function IsItemUsable(ItemName)

	local rlt=0;
	--MyTricksLog("function IsItemUsable() ; ItemName="..ItemName);
	if GetItemCount(ItemName,nil,nil) > 0 then
		MyTricksLog("function IsItemUsable() ; GetItemCount()>0");
		local start, duration, enable=GetItemCooldown(ItemName); 
		if start == 0 then
			rlt = 1;
		end
	end
	return rlt;
end

local function IsSpellUsable(SpellName)
	MyTricksLog("function IsSpellUsable() ; SpellName="..SpellName);
	
	local rlt=0;
	local start, duration, enable=GetSpellCooldown(SpellName); 
	if start == 0 then
		rlt = 1;
	end
	
	return rlt;
end

local function IsInventoryUsable(InventorySlotId)
	local start, duration, enable=GetInventoryItemCooldown("player",InventorySlotId);
	if start == 0 then
		return 1;
	else
		return 0;
	end
end

local iActionCount=1;
local function BindAction(iType,sKey,...)
	local sVerb="";
	if iType == 1 then
		sVerb="/use " ;
	elseif iType == 2 then
		sVerb="/cast " ;
	elseif iType == 3 then
		sVerb="/use " ;
	end
	
	local sMacroBody="";
	for i=1,select("#", ...) do
		sMacroBody=sMacroBody..sVerb..select(i, ...).."\n";
	end
	MyTricksLog("function BindAction() ; sMacroBody="..sMacroBody..",MacroName=StrugAction"..iActionCount);
	local index=GetMacroIndexByName("StrugAction"..iActionCount);
	if index ~= 0 then
		DeleteMacro("StrugAction"..iActionCount);
	end
	CreateMacro("StrugAction"..iActionCount, 71, sMacroBody, 1,nil);
	SetBindingMacro(sKey,"StrugAction"..iActionCount);
	
	iActionCount=iActionCount+1;
end

local function BindStrugAction() 
	MyTricksLog("BindStrugAction");
	if 1 ~= iActionCount then
		local index=GetMacroIndexByName("StrugAction"..iActionCount);
		if 0 ~= index then
			return;
		else
			iActionCount = 1;
		end
	end 
	BindAction(1,"NUMPAD1","极效治疗石");
	BindAction(1,"NUMPAD2","特效治疗石");
	BindAction(2,"NUMPAD3","盾墙","盾牌格挡","破釜沉舟"); 
	BindAction(1,"NUMPAD4","超级治疗药水"); 
	BindAction(3,"NUMPAD5","13","14"); 
	BindAction(1,"NUMPAD6","特效治疗药水"); 
	
	local index=GetMacroIndexByName("ClearStrugInfo");
	if index == 0 then
		CreateMacro("ClearStrugInfo", 72, "/run MtCodeFrm:Hide();", 1,nil);
	end	
	SetBindingMacro("NUMPAD0","ClearStrugInfo");
	
	local index=GetMacroIndexByName("SelectStrugAction");
	if index == 0 then
		CreateMacro("SelectStrugAction", 73, "/autostrug", 1,nil);
	end
end 

local function SelectStrugAction() 
	MyTricksLog("SelectStrugAction");
	local bNoAction = 0;
	if IsItemUsable("极效治疗石") ~= 0 then 
		MtEncode(20001);
		MyTricksLog("select 极效治疗石");
	elseif IsItemUsable("特效治疗石") ~= 0 then 
		MtEncode(20002);
		MyTricksLog("select 特效治疗石");
	elseif IsSpellUsable("盾墙") ~= 0 or IsSpellUsable("破釜沉舟") ~= 0 then
		MtEncode(20003);
		MyTricksLog("select 破釜沉舟");
	elseif IsItemUsable("超级治疗药水") ~= 0 then 
		MtEncode(20004);
		MyTricksLog("select 超级治疗药水");
	elseif IsInventoryUsable(13) ~= 0 then 
		MtEncode(20005);
		MyTricksLog("select 饰品");
	elseif IsItemUsable("特效治疗药水") ~= 0 then 
		MtEncode(20006);
		MyTricksLog("select 特效治疗药水");
	else
		bNoAction = 1;
		MyTricksLog("Do Nothing");
	end	
	if 0 == bNoAction then
		MtCodeFrm:Show()
	end
end 

SLASH_AUTOSTRUG1 = "/autostrug";   
SlashCmdList["AUTOSTRUG"] = SelectStrugAction; 



function BindCountItemPushMacro()
	--InitReceiveEventFrm:RegisterEvent("UNIT_COMBAT"); 
	--MyTricksLog("InitReceiveEventFrm ")
	local index=GetMacroIndexByName("countitempush");
	if 0 == index then
		CreateMacro("countitempush", 81, "/countitempush", 1,nil);
	end
	SetBindingMacro("F10","countitempush");
	index=GetMacroIndexByName("nocountitempush");
	if 0 == index then
		CreateMacro("nocountitempush", 82, "/nocountitempush", 1,nil);
	end
	SetBindingMacro("F12","nocountitempush");
	
	index=GetMacroIndexByName("SwitchBaitUse");
	if 0 == index then
		CreateMacro("SwitchBaitUse", 83, "/run SwitchBaitUse()", 1,nil);
	end
end

function ReceiveEventFrmOnLoad()
	ReceiveEventFrm:RegisterEvent("VARIABLES_LOADED");
	ReceiveEventFrm:RegisterEvent("UNIT_COMBAT");
	ReceiveEventFrm:RegisterEvent("ITEM_PUSH");
	ReceiveEventFrm:RegisterEvent("ADDON_LOADED");
	ReceiveEventFrm:RegisterEvent("UNIT_AURA");
end

function OnAddonLoaded()
	BindCountItemPushMacro();
	BindStrugAction();
	BindExpectedProfitCompute();
end

function HandCombatEvent(self,event,...)
	local arg={};
	MyTricksLog("HandCombatEvent ");
	--[[
	if event == "UNIT_COMBAT" then
		for i = 1, select('#', ...) do
			arg[i]=select(i, ...);
    end
	end
	if arg[1] == "player" and arg[2] == "WOUND" then
		BindStrugAction();
	end
	]]
end

function MoniterAura(self,event,...)
	unit = ...; 
	if unit == "player" then
		--CheckBait(); 
	end 
end